package com.xwn.config;

import org.springframework.beans.factory.annotation.Configurable;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

@Configurable
public class JdbcConfig {
    private String driver;
    private String url;
    private String user;
    private String password;

    private static JdbcConfig jdbcConfig;

    private JdbcConfig(String driver, String url, String user, String password) {
        this.driver = driver;
        this.url = url;
        this.user = user;
        this.password = password;
    }

    public JdbcConfig initJdbcConfig() {
        Properties prop = new Properties();
        try {
            prop.load(new FileInputStream(new File("resource/jdbc.properties")));
        } catch (IOException e) {
            throw new RuntimeException("jdbc配置文件读取失败");
        }
        return getInstance(prop.getProperty("driver"),prop.getProperty("url"),prop.getProperty("user"),prop.getProperty("password"));
    }

    private JdbcConfig getInstance(String driver,String url,String user,String password){
        synchronized (this.getClass()) {
            if (jdbcConfig == null) {
                return jdbcConfig = new JdbcConfig(driver,url,user,password);
            }
        }
        return jdbcConfig;
    }

    public String getDriver() {
        return driver;
    }

    public void setDriver(String driver) {
        this.driver = driver;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
